<?php
namespace app\admin\controller;
use think\View;
use think\Cache;
use org\Auth;
use think\Session;
use think\Config;
use think\Controller;

class Base extends \think\Controller
{
    public function __construct()
    {
        parent::__construct();

        //栏目循环
        $term_info = D('term')->selectAll();
        $fen_ji = $this->getTermTree(0,$term_info);
        $this->view->assign('term_info',$fen_ji);

        $select_all_parent_id = M('term')->field('parent_id')->select();
        if($select_all_parent_id){

            foreach( $select_all_parent_id as $key=>$value ){
                $result[] = $value['parent_id'];
            }
            $this->view->assign('result',$result);
        }
    }

		    protected function isLogin()
    {

        if(false == session('user') || time()-session('lastAct')>C('login_timeout')){
            $this->redirect('Sign/in');
            exit();
        }
        session('lastAct', time());
    }

    /**
     * 重构生成树的方法
     * @param int $topId 顶级父栏目ID
     * @param array $termArr  栏目数组
     * @param str $repeat 每层添加的前缀
     * 马宇翔
     */
    protected function getTermTree($topId = 0, $termArr = [], $repeat = '||&nbsp;&nbsp;&nbsp;&nbsp;')
    {
        if(empty($termArr)) return [];
        $newTermArr = $childArr = [];
        foreach($termArr as $termInfo)
            $childArr[$termInfo['parent_id']][] = $termInfo;
        $funGet = function ($id, $strRepeat) use (&$newTermArr, $childArr, $repeat, &$funGet) {
            foreach($childArr[$id] as $termInfo){
                $termInfo['name'] = $strRepeat.$termInfo['name'];
                $termInfo['hasChild']= isset($childArr[$termInfo['id']])? 1: 0;
                $newTermArr[] = $termInfo;
                if($termInfo['hasChild'])
                    $funGet($termInfo['id'], $termInfo['name'].'->');
            }
        };
        $funGet($topId, '');//闭包引用传入$newTermArr
        return $newTermArr;
    }





    /**
     * @param $content  确认信息的内容
     * @param $flag     确认参数标识
     * @param $keyfalg  PostKey参数标识
     * @result bool
     */
    protected function reconfirm($msg='', $flag='reconfirm', $keyflag='rcfkey')
    {
        $reconfirm = I('get.'.$flag);
        if($reconfirm === '0')
            return false;
        if($reconfirm === '1'){
            $key = I('get.'.$keyflag);
            $server = unserialize(cookie($key.'server'));
            $_SERVER = array_merge($_SERVER, $server);
            $_POST = unserialize(cookie($key.'post'));
            return true;
        }
        $vals = I('get.');
        $key = md5(time().'reconfirm');
        cookie(['expire'=>600]);
        $server['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
        cookie($key.'server', serialize($server));
        cookie($key.'post', serialize($_POST));
        $vals = array_merge($vals, [$keyflag=>$key]);
        $this->assign('vals', $vals);
        $this->assign('flag', $flag);
        $this->assign('msg', $msg);
        $tpl = C('dispatch_reconfirm_tmpl');
        $tpl = $tpl? $tpl: 'public/reconfirm';
        echo $this->fetch($tpl);
        exit;
    }

    /**
     * @param $count  传入所取信息个数
     * @param $pagecount  每页个数
     * @param $page  模板调用
     * Author: yanli
     */
    protected function getPage($count,$pagecount=2)
    {
        $getPage = new \org\Page($count,$pagecount);
        $theme = "%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%";
        $getPage->setConfig('theme',$theme);
        $page = $getPage->show();
        $this->view->assign('page',$page);
        return $getPage;
    }

    /**
     * @param  namespace org;
     * Author: yanli
     */
    public function verify()
    {
        $config =    array(
            'fontSize'    =>    14,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'codeSet' =>'0123456789',
        ); 
        $Verify = new \org\Verify($config);
        ob_end_clean();
        $Verify->entry();   
    }

    /**
     * @param  namespace org;
     * @param  一键清理缓存;
     * Author: yanli
     */

    public function clear_cache()
    {
        if(Cache::clear()){
            return $this->success('清除缓存成功', U('Index/index')); 
        }

    }

    public function get_tree($rows)
    {

        foreach ($rows as &$row)
        {

            $num=substr_count($row['path'],'-');

            if($row['pid']>0){
                $pre='| '.str_repeat('--',$num);
            }else{
                $pre='';
            }
            @$row['tree']=$pre.$row['title'];

        }

        foreach($rows as $row2){
            $arr[]=$row2['path'].'-'.$row2['id'];
        }

        array_multisort($arr,$rows);
        return $rows;
    }
}

